package logic

import (
	"context"
	"fmt"
	"yuni/model/mysql"

	"yuni/yunirpc/internal/svc"
	"yuni/yunirpc/yunirpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewProductAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductAddLogic {
	return &ProductAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ProductAddLogic) ProductAdd(in *yunirpc.ProductAddRequest) (*yunirpc.ProductAddResponse, error) {
	// todo: add your logic here and delete this line

	product := mysql.Product{
		Title:   in.Title,
		Price:   float64(in.Price),
		Stock:   in.Stock,
		UserId:  in.UserId,
		ClassId: in.ClassId,
	}
	if in.Title == "" || in.Price == 0 || in.Stock == 0 {
		return nil, fmt.Errorf("发布时验证标题、分类、价格、描述等必填项")
	}
	mysql.Db.Debug().Create(&product)

	return &yunirpc.ProductAddResponse{
		ProductId: int64(product.ID),
	}, nil
}
